package com.mall.common;

import java.util.List;

public class WebResultHelper {

	// 成功
	public static final Integer SUCCESS = 0;

	// 失败
	public static final Integer ERROR = 1;

	//使用错误码
	public static final int OTHER_ERROR = 600;// 其它异常错误

	static WebResult build(Integer errCode, String errMsg) {
		WebResult result = new WebResult();
		result.setCode(errCode);
		result.setMsg(errMsg);
		return result;
	}

	public static WebResult otherError(Exception ex) {
		return build(OTHER_ERROR, ex != null ? ex.getMessage() : "其它错误");
	}

	public static WebResult success() {
		WebResult result = new WebResult();
		result.setCode(SUCCESS);
		result.setMsg("success");
		return result;
	}

	public static WebResult success(String msg) {
		WebResult result = new WebResult();
		result.setCode(SUCCESS);
		result.setMsg(msg);
		return result;
	}

	public static WebResult error(String msg) {
		WebResult result = new WebResult();
		result.setCode(ERROR);
		result.setMsg(msg);
		return result;
	}

	public static <T> WebResult<T> success(List<T> data, int totalRecord) {
		WebResult<T> result = new WebResult<T>();
		result.setCode(SUCCESS);
		result.setMsg("success");
		result.setCount(totalRecord);
		result.setData(data);
		return result;
	}

	public static <T> WebResult<T> success(T dataSingle) {
		WebResult<T> result = new WebResult<T>();
		result.setCode(SUCCESS);
		result.setMsg("success");
		result.setDataSingle(dataSingle);
		return result;
	}

	public static <T> WebResult<T> success(T dataSingle, String msg) {
		WebResult<T> result = new WebResult<T>();
		result.setCode(SUCCESS);
		result.setMsg(msg);
		result.setDataSingle(dataSingle);
		return result;
	}

}